package com.yu.userinfo;


import com.yu.dao.DBProvider;
import com.yu.utils.MyUtils;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class ModelUserInfo {
    /**
     * 登录业务逻辑处理
     *
     * @param userInfoBean 参数实体类
     * @return 登陆验证结果
     */
    public boolean doLogin(UserInfoBean userInfoBean) {
        // 登录业务逻辑验证处理
        // 登录查询的sql语句
        String strSql = "SELECT * FROM userinfo WHERE userName = ? AND userPsw = ?";
        // 调用dao层进行数据库操作
        List<UserInfoBean> lstUsers = MyUtils.getNewInstance(DBProvider.class).
                query(strSql, new BeanListHandler<>(UserInfoBean.class), new Object[]{userInfoBean.getUserName(), userInfoBean.getUserPsw()});
        // 返回结果
        return lstUsers != null && lstUsers.size() > 0;
    }

    /**
     *
     * @param userInfoBean
     * @return
     */
    public List<UserInfoBean> queryUserinfoByParams(UserInfoBean userInfoBean) {
        List<UserInfoBean> lstUsers = null;
        String strSql="select * from userinfo";
        //TODO
        /*
        * SELECT * FROM userinfo where userIsDelete = 0
        * and username like '%ad%'
        * and userTel like '%150%'
        * and userAddress like '%山西%'
        * and userBirthday > '2021-06-01'
        * and userBirthday <'2021-06-04'
        * and userRegDate > '2021-05-01'
        * and userRegDate < '2021-06-04'
        * */
        StringBuilder stringBuilder = new StringBuilder(" SELECT * FROM userinfo where userIsDelete = 0 ");
        List<Object> lstParams = new ArrayList<>();
        if(StringUtils.isNotEmpty(userInfoBean.getUserName())) {
            lstParams.add("%"+userInfoBean.getUserName()+"%");
            stringBuilder.append("and username like ?");
        }
        if (StringUtils.isNotEmpty(userInfoBean.getUserTel())) {
            stringBuilder.append(" and userTel like ? ");
            // 保存对应的参数
            lstParams.add("%" + userInfoBean.getUserTel() + "%");
        }

        if (StringUtils.isNotEmpty(userInfoBean.getUserAddress())) {
            stringBuilder.append(" and userAddress like ? ");
            // 保存对应的参数
            lstParams.add("%" + userInfoBean.getUserAddress() + "%");
        }

        if (userInfoBean.getUserBirthday() != null) {
            stringBuilder.append(" and userBirthday >= ? ");
            // 保存对应的参数
            lstParams.add(userInfoBean.getUserBirthday());
        }

        if (userInfoBean.getUserBirthday2() != null) {
            stringBuilder.append(" and userBirthday <= ? ");
            // 保存对应的参数
            lstParams.add(userInfoBean.getUserBirthday2());
        }

        if (userInfoBean.getUserRegDate() != null) {
            stringBuilder.append(" and userRegDate >= ? ");
            // 保存对应的参数
            lstParams.add(userInfoBean.getUserRegDate());
        }

        if (userInfoBean.getUserRegDate2() != null) {
            stringBuilder.append(" and userRegDate <= ? ");
            // 保存对应的参数
            lstParams.add(userInfoBean.getUserRegDate2());
        }
        // 输出sql语句和参数信息
        System.out.println("查询SQL：" + stringBuilder.toString());
        System.out.println("查询参数：" + lstParams);
        lstUsers = MyUtils.getNewInstance(DBProvider.class).query(strSql, new BeanListHandler<>(UserInfoBean.class), null);
        lstUsers = MyUtils.getNewInstance(DBProvider.class)
                .query(stringBuilder.toString(), new BeanListHandler<>(UserInfoBean.class), lstParams.toArray());
        // 返回值
        return lstUsers;

    }

}
